﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.SqlClient;
using System.Text;
using StoreVnCore.Entity;
using StoreVnCore.Common;
using StoreVnCore.Interface;
using StoreVnCore.ExceptionManager;
using System.Data;

namespace StoreVnCore.DataAccessObject
{
    /// <summary>
    /// Class execute access database of table ContentFormat
    /// </summary>
    public class ContentFormatDAO : BaseDAO, IContentFormatDAO
    {
        #region Member
        protected const string STORE_INSERT = "StoreVn_Sp_ContentFormat_Insert";
        protected const string STORE_UPDATE = "StoreVn_Sp_ContentFormat_Update";
        protected const string STORE_DELETE = "StoreVn_Sp_ContentFormat_Delete";
        protected const string STORE_SELECTONE = "StoreVn_Sp_ContentFormat_SelectOneById";
        protected const string STORE_SELECTALL = "StoreVn_Sp_ContentFormat_SelectAll";
        protected const string STORE_SELECTBYContentFormatID = "StoreVn_Sp_ContentFormat_SelectByTypeOfContent";
        #endregion

        #region Method
        /// <summary>
        /// Execute insert data into database of table ContentFormat
        /// </summary>
        public ContentFormatEntity Insert(ContentFormatEntity entity)
        {
            #region Parameter
            SqlParameter[] prm = new SqlParameter[4];
            prm[0] = new SqlParameter(FieldNames.ContentFormat.Id, SqlDbType.Int, 4);
            prm[0].Direction = ParameterDirection.Output;
            prm[1] = new SqlParameter(FieldNames.ContentFormat.TypeOfContent, entity.TypeOfContent);
            prm[2] = new SqlParameter(FieldNames.ContentFormat.Name, entity.Name);
            prm[3] = new SqlParameter(FieldNames.ContentFormat.Description, entity.Description);
            #endregion
            #region Execute
            try
            {
                ExeNonQuery(STORE_INSERT, prm);
            }
            catch (Exception ex)
            {
                throw new DataAccessException("ContentFormatDAO::Insert::Error", ex);
            }
            entity.Id = (int)prm[0].Value;
            #endregion
            return entity;
        }


        /// <summary>
        /// Execute update data into database of table ContentFormat
        /// </summary>
        public ContentFormatEntity Update(ContentFormatEntity entity)
        {
            #region Parameter
            SqlParameter[] prm = new SqlParameter[4];
            prm[0] = new SqlParameter(FieldNames.ContentFormat.Id, entity.Id);
            prm[1] = new SqlParameter(FieldNames.ContentFormat.TypeOfContent, entity.TypeOfContent);
            prm[2] = new SqlParameter(FieldNames.ContentFormat.Name, entity.Name);
            prm[3] = new SqlParameter(FieldNames.ContentFormat.Description, entity.Description);
            #endregion
            #region Execute
            try
            {
                ExeNonQuery(STORE_UPDATE, prm);
            }
            catch (Exception ex)
            {
                throw new DataAccessException("ContentFormatDAO::Update::Error", ex);
            }
            #endregion
            return entity;
        }


        /// <summary>
        /// Execute delete data from database of table ContentFormat
        /// </summary>
        public void Delete(int Id)
        {
            #region Parameter
            SqlParameter[] prm = new SqlParameter[1];
            prm[0] = new SqlParameter(FieldNames.ContentFormat.Id, Id);
            #endregion
            #region Execute
            try
            {
                ExeNonQuery(STORE_DELETE, prm);
            }
            catch (Exception ex)
            {
                throw new DataAccessException("ContentFormatDAO::Delete::Error", ex);
            }
            #endregion
        }


        /// <summary>
        /// Execute select one record from database of table ContentFormat
        /// </summary>
        public ContentFormatEntity SelectOne(int Id)
        {
            ContentFormatEntity entity = null;
            DataTable dt = null;
            #region Parameter
            SqlParameter[] prm = new SqlParameter[1];
            prm[0] = new SqlParameter(FieldNames.ContentFormat.Id, Id);
            #endregion
            #region Execute
            try
            {
                dt = ExeDataTable(STORE_SELECTONE, prm);
            }
            catch (Exception ex)
            {
                throw new DataAccessException("ContentFormatDAO::SelectOne::Error", ex);
            }
            #endregion
            #region BindData to Entity
            if (dt != null && dt.Rows.Count > 0)
            {
                entity = new ContentFormatEntity();
                DataRow dr = dt.Rows[0];
                //Id
                if (dr[FieldNames.ContentFormat.Id] != null && dr[FieldNames.ContentFormat.Id].ToString() != "")
                {
                    entity.Id = (int)dr[FieldNames.ContentFormat.Id];
                }

                //TypeOfContent
                if (dr[FieldNames.ContentFormat.TypeOfContent] != null && dr[FieldNames.ContentFormat.TypeOfContent].ToString() != "")
                {
                    entity.TypeOfContent = (string)dr[FieldNames.ContentFormat.TypeOfContent];
                }

                //Name
                if (dr[FieldNames.ContentFormat.Name] != null && dr[FieldNames.ContentFormat.Name].ToString() != "")
                {
                    entity.Name = (string)dr[FieldNames.ContentFormat.Name];
                }

                //Description
                if (dr[FieldNames.ContentFormat.Description] != null && dr[FieldNames.ContentFormat.Description].ToString() != "")
                {
                    entity.Description = (string)dr[FieldNames.ContentFormat.Description];
                }
            }
            #endregion
            return entity;
        }


        /// <summary>
        /// Execute select all data from database of table ContentFormat
        /// </summary>
        public DataTable SelectAll()
        {
            DataTable dt = null;
            #region Execute
            try
            {
                dt = ExeDataTable(STORE_SELECTALL);
            }
            catch (Exception ex)
            {
                throw new DataAccessException("ContentFormatDAO::SelectAll::Error", ex);
            }
            #endregion
            return dt;
        }

        /// <summary>
        /// Execute select all data from database of table ContentFormat
        /// </summary>
        public DataTable SelectByTypeOfContent(string TypeOfContent)
        {
            DataTable dt = null;
            #region Parameter
            SqlParameter[] prm = new SqlParameter[1];
            prm[0] = new SqlParameter(FieldNames.ContentFormat.TypeOfContent, TypeOfContent);
            #endregion
            #region Execute
            try
            {
                dt = ExeDataTable(STORE_SELECTBYContentFormatID, prm);
            }
            catch (Exception ex)
            {
                throw new DataAccessException("ContentFormatDAO::SelectAll::Error", ex);
            }
            #endregion
            return dt;
        }

        #endregion
    }
}
